<?php

/**
 * Shorten directory separator.
 *
 * @var string
 */
define('DS', DIRECTORY_SEPARATOR);
/**
 * Constant for namespace separator.
 *
 * @var string
 */
define('NS', '\\');
/**
 * Root directory.
 *
 * @var string
 */
define('ROOT_DIR', dirname(__DIR__));
/**
 * Classes directory.
 *
 * @var string
 */
define('CLASSES_DIR', ROOT_DIR . DS . 'classes');
/**
 * Traits directory.
 *
 * @var string
 */
define('TRAITS_DIR', ROOT_DIR . DS . 'traits');
/**
 * Intefaces directory.
 *
 * @var string
 */
define('INTERFACES_DIR', ROOT_DIR . DS . 'interfaces');
/**
 * Tests directory.
 *
 * @var string
 */
define('TESTS_DIR', ROOT_DIR . DS . 'tests');

/**
 * Returns a relative path filename for a namespaced object.
 *
 * @param string $objectName A namespaced object name.
 * @return string A string representing the relative path of a namespaced object file.
 */
function getRelativePathFilename($objectName)
{
    return str_replace(NS, DS, $objectName) . '.php';
}

/**
 * Autoloads a class file for a namespaced class name.
 *
 * @param string $className A namespaced class name.
 * @return boolean Returns TRUE after loading the class file on success or FALSE on failure.
 */
function divineAutoload($className)
{
    $objectDirs   = array(CLASSES_DIR, TRAITS_DIR, INTERFACES_DIR, TESTS_DIR);
    $relativePath = getRelativePathFilename($className);

    foreach ($objectDirs as $dir)
    {
        $absolutePath = $dir . DS . $relativePath;

        if (file_exists($absolutePath))
        {
            require $absolutePath;

            return TRUE;
        }
    }

    return FALSE;
}

spl_autoload_register('divineAutoload');